# ======================================================================
#
# Copyright (C) ADN
# Set.pl
# Description:
#  - Permet de configurer la Sonde
#  - Help to configure the Sonde
#
# ======================================================================
use strict;
use warnings;
use XML::Simple;
use ADN::CONF::Read;
use File::Copy;

if ( eval "use Win32; 1" ) {};

if ( @ARGV ) {
        if ( $ARGV[0] eq "-h" || $ARGV[0] eq "--help" || $ARGV[0] eq "/?" ) {
                print "Help : perldoc $0" . "\n";
                exit 0;
        }
}

my @list;
my ( $smp, $d, $d2, $conf, $file, $en, $fr, $cf, $path );

# #######################
# PRINT THE LOGO
# #######################
print <<MESS;
\t\t     **      *******    ****     **
\t\t    ****    /**////**  /**/**   /**
\t\t   **//**   /**    /** /**//**  /**
\t\t  **  //**  /**    /** /** //** /**
\t\t ********** /**    /** /**  //**/**
\t\t/**//////** /********  /**   //****
\t\t//      //  ////////   ///    ////
\t\t\t    Configuration    \t(c) Copyright 2009 - ADN
\t\t
MESS

# #######################
# CONFIG THE PATH
# #######################
$cf = ADN::CONF::Read->new();
$path = $cf->_send_path();

# #######################
# CONFIG THE MENU
# #######################
$conf = "$path/conf.xml";
$fr = "$path/LANG/conf_fr.xml";
$en = "$path/LANG/conf_en.xml";

@list = ( "display", 
	  "ip", 
	  "port", 
	  "protect", 
	  "menu",
	  "log", 
	  "proc", 
	  "restart",
	  "end" );

# #######################
# LOAD LANGAGE
# #######################
$file = langage();

# #######################
# LOAD XML FILE
# #######################
$smp = XML::Simple->new( NoSort => '1', 
			 NoAttr => '1' );
$d = XMLin( $file );
$d2 = XMLin( $conf );

# #######################
# CONFIG THE EXECUTABLE
# #######################
if ( $^O eq "MSWin32" ) {
        xml( "service_server",
             "bin",
             "perl.exe" );
}
else {
        xml( "service_server",
             "bin",
             "perl" );
}

_start();

# #######################
# SELECT THE LANGAGE
# #######################
sub langage {
    my $v;
    print <<MESS;
    \t\t 0. Francais
    \t\t 1. English
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    if ( $v =~ m/^0$/i ) {
	$file = $fr;
	 return $file;   
    }
    if ( $v =~ m/^1$/i ) {
	$file = $en;
	return $file;
    }
    else {
	langage();
    }
}

# #######################
# READ THE MENU
# #######################
sub _start {
    my $i; 
    foreach ( @list ) {
	if ( $_ eq "end" && $d2->{service}->{display} == 0 ) {
	    service();
	} 
	if ( $_ eq "end" ) {
	    last;
	}
	if ( $_ eq "display" ) {
		print $d->{$_}->{title} . "\n";
		print "Desc : " . $d->{$_}->{desc} . "\n";
		display();
		next;
	}
	print $d->{$_}->{title} . "\n";
	print "Desc : " . $d->{$_}->{desc} . "\n";
	if ( $_ eq "ip" ) {
	    	ip();
	    	next;
	}
	if ( $_ eq "port") {
	    	port();
	    	next;
	}
	if ( $_ eq "protect") {
	    	protect( $_ );
	    	next;
	}
	if ( $_ eq "menu") {
	    	$i = menu();
	    	if ( $i == 0 ) {
			last;
	    	}
	    	next;
	}
	answer( $_ );
    }
    print <<MESS;
$d->{end}->{a}
$d->{end}->{b}
MESS
    sleep 2;
}

# #######################
# MENU FOR THE DISPLAY
# #######################
sub display {
    my ( $v, $a, $wrap, $perl, $serv );
    if ( $d2->{service}->{display} == 0 ) {
	$a = $d->{display}->{a};
    }
    else {
	$a = $d->{display}->{b};
    }
    print <<MESS;
    \t\t 0. $d->{display}->{a}
    \t\t 1. $d->{display}->{b}
    [ $a ] 
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    chop $path;
    if ( $^O eq "MSWin32" ) {	
	$path = Win32::GetShortPathName( "$path" );
    	$wrap = Win32::GetFullPathName( "$path/../../../../wrap.exe" );
    	$perl = Win32::GetFullPathName( "$path/../../../bin/perl.exe" );
    	$serv = <<MESS;
sc create adnsonde binpath= "$wrap $perl $path/SERVER/Server.pl" displayname= "AD-Network Sonde"
MESS
    	$serv =~ tr/\//\\/;
    }
    else {
    	$serv = linux();
    }    
    if ( $v eq "" ) {
	xml( "service", "display",  $d2->{service}->{display} );
	if ( $d2->{service}->{display} == 0 ) {
	    system( $serv );
	}
	return;
    }
    if ( $v =~ m/^0$/i ) {
	xml( "service", 
	     "display", 
	     $v );
	system( $serv );
	return;
    }
    if ( $v =~ m/^1$/i ) {
	xml( "service", 
	     "display", 
	     $v );
	return;
    }
    else {
	display();
    }
}

# #######################
# DISTRIB LINUX
# #######################
sub linux {
	my $cmd;
	if ( -e "/etc/debian_version" ) {
		copy ( "$path/HELP/adnsonde.conf", "/etc/init/adnsonde.conf" );		
		copy ( "$path/HELP/adnsonde.sh", "/etc/init.d/adnsonded" );
		system ("chmod +x /etc/init.d/adnsonded");
		$cmd = "update-rc.d -f adnsonded start 71 2 3 4 5 . stop 31 0 1 6 .";
	}
	else {
		$cmd = "chkconfig --add adnsonded";
	}
	return $cmd;
}

# #######################
# MENU FOR THE SERVICE
# #######################
sub service {
    my $v;
    print <<MESS;
$d->{service}->{title}
Desc : $d->{service}->{desc}
    \t\t 0. $d->{service}->{a}
    \t\t 1. $d->{service}->{b} 
    [ $d->{service}->{a} ]
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    if ( $v eq "" || $v =~ m/^0$/i ) {
	if ( $^O eq "MSWin32" ) {	
		system( "net start adnsonde" );
	}
	else {
		system ( "/etc/init.d/adnsonde start" );
	}
	return;
    }
    if ( $v =~ m/^1$/i ) {
	return;
    }
    else {
	service();
    }
}

# #######################
# MENU FOR THE IP
# #######################
sub ip {
    my $v;
    print <<MESS;
    [ $d2->{server}->{address} ] 
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    if ( $v eq "" ) {
	xml( "server", 
	     "address", 
	     $d2->{server}->{address} );
	return;
    }
    if ( $v =~ m/^([0-9]{1,3}\.){3}[0-9]{1,3}$/i ) {
	xml( "server", 
	     "address", 
	     $v );
	return;
    }
    else {
	print "Usage : 127.0.0.1" . "\n";
	ip();
    }
}

# #######################
# MENU FOR THE PORT
# #######################
sub port {
    my $v;
    print <<MESS;
    [ $d2->{server}->{port} ] 
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    if ( $v eq "" ) {
	xml( "server", 
	     "port", 
	     $d2->{server}->{port} );
	return;
    }
    if ( $v =~ m/^[0-9]{1,5}$/i ) {
	xml( "server", 
	     "port", 
	     $v );
	return;
    }
    else {
	print "Usage : 4242" . "\n";
	port();
    }
}

# #######################
# MENU FOR THE WEB PROTECTION
# #######################
sub protect {
    my ( $v, $v2, $a );
    $v = $_;
    if ( $d2->{$d->{$v}->{n}}->{$d->{$v}->{v}} == 0 ) {
	$a = $d->{choice}->{a};
    }
    else {
	$a = $d->{choice}->{b};
    }
    print <<MESS;
    \t\t 0. $d->{choice}->{a}
    \t\t 1. $d->{choice}->{b} 
    [ $a ]
MESS
print "> ";
    $v2 = <stdin>;
    chop $v2;
    if ( $v2 eq "" ) {
	if ( $d2->{$d->{$v}->{n}}->{$d->{$v}->{v}} == 0 ) {
	    xml( $d->{$v}->{n}, 
		 $d->{$v}->{v}, 
		 $d2->{$d->{$v}->{n}}->{$d->{$v}->{v}} );
	    return;
	}
	else {
	    xml( $d->{$v}->{n}, 
		 $d->{$v}->{v}, 
		 $d2->{$d->{$v}->{n}}->{$d->{$v}->{v}} );
	    password();
	    return;
	}
    }
    if ( $v2 =~ m/^0$/i ) {
	xml( $d->{$v}->{n}, 
	     $d->{$v}->{v}, 
	     "0" );
	return;
    }
    if ( $v2 =~ m/^1$/i ) {
	xml( $d->{$v}->{n}, 
	     $d->{$v}->{v}, 
	     "1" );
	password();
	return;
    }
    else {
	print $d->{$v}->{title} . "\n";
	print "Desc : " . $d->{$v}->{desc} . "\n";
	answer( $v );
    }
}

# #######################
# MENU FOR THE PASSWORD
# #######################
sub password {
    my ( $v, $v2 );
    print <<MESS;
$d->{password}->{title}
Desc : $d->{password}->{desc}
    [ $d2->{web}->{password} ] 
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    if ( $v eq "" ) {
	xml( "web", 
	     "password", 
	     $d2->{web}->{password} );
	return;
    }
    if ( $v ne "" ) {
	print $d->{password}->{a} . " > ";
	$v2 = <stdin>;
	chop $v2;
	if ( $v eq $v2 ) {
	    xml( "web", 
		 "password", 
		 $v );
	    return;
	}
	else {
	    print "Usage : adn42" . "\n";
	    port();
	}
    }
}

# #######################
# MENU FOR THE MENU WEB
# #######################
sub menu {
    my $v;
    print <<MESS;
    \t\t 0. $d->{choice}->{a}
    \t\t 1. $d->{choice}->{b} 
    [ $d->{choice}->{b} ]
MESS
    print "> ";
    $v = <stdin>;
    chop $v;
    if ( $v eq "" || $v =~ m/^1$/i ) {
	return 1;
    }
    if ( $v =~ m/^0$/i ) {
	xml( "web", 
	     "log", 
	     "0" );
	xml( "web", 
	     "proc", 
	     "0" );
	xml( "web", 
	     "restart", 
	     "0" );
	return 0;
    }
    else {
	print <<MESS;	
$d->{menu}->{title}
Desc : $d->{menu}->{desc}
MESS
        menu();
    }
}

# #######################
# MENU FOR ALL OTHER
# #######################
sub answer {
    my ( $v, $v2, $a );
    $v = $_;
    if ( $d2->{$d->{$v}->{n}}->{$d->{$v}->{v}} == 0 ) {
	$a = $d->{choice}->{a};
    }
    else {
	$a = $d->{choice}->{b};
    }
    print <<MESS;
    \t\t 0. $d->{choice}->{a}
    \t\t 1. $d->{choice}->{b} 
    [ $a ]
MESS
    print "> ";
    $v2 = <stdin>;
    chop $v2;
    if ( $v2 eq "" ) {
	xml( $d->{$v}->{n}, 
	     $d->{$v}->{v}, 
	     $d2->{$d->{$v}->{n}}->{$d->{$v}->{v}} );
	return;
    }
    if ( $v2 =~ m/^0$/i ) {
	xml( $d->{$v}->{n}, 
	     $d->{$v}->{v}, 
	     "0" );
	return;
    }
    if ( $v2 =~ m/^1$/i ) {
	xml( $d->{$v}->{n}, 
	     $d->{$v}->{v}, 
	     "1" );
	return;
    }
    else {
	print <<MESS;	
$d->{$v}->{title}
Desc : $d->{$v}->{desc}
MESS
        answer( $v );
    }
}

# #######################
# WRITE THE XML
# #######################
sub xml {
    my ( $smp, $d, $b1, $b2, $v );
    ( $b1, $b2, $v ) = @_;
    $smp = XML::Simple->new( NoSort => '1', 
			     NoAttr => '1' );
    $d = XMLin( $conf );
    $d->{$b1}->{$b2} = $v;
    $smp->XMLout( $d,
		  NoAttr => '1',
		  NoSort => '0',
		  RootName => 'configuration',
		  OutputFile => $conf );
    return;
}
1;
__END__

=pod

=head1 NAME

Conf.pl - Perl program to help you configure the Sonde module part of the AD-Network Project

=head1 SYNOPSIS

./Conf.pl

That's it.

=head1 DESCRIPTION

Conf.pl will edit the configuration of the ADN::SERVER::Server modular. 

=head1 AUTHOR

ADN TEAM, E<lt>support@ad-network.euE<gt>

=head1 BUG REPORT

Use TRAC system to report bug or ask for features:
L<http://ad-network.eu/trac/newticket>

=head1 DOWNLOAD

ADN SOnde is available at
L<http://ad-network.eu/web/index.php/produit.html?art=dl>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by ADN TEAM, E<lt>support@ad-network.euE<gt>

This software is under the LGPL
L<http://www.gnu.org/licenses/lgpl-3.0.txt>
L<http://www.gnu.org/licenses/lgpl.html>

=cut
